package com.example.rlw.mvpplugintest.downfile;

import android.Manifest;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Toast;

import com.example.rlw.mvpplugintest.R;
import com.example.rlw.mvpplugintest.mvp.MVPBaseActivity;
import com.example.rlw.mvpplugintest.utils.NotificationUtils;
import com.example.rlw.mvpplugintest.utils.PermissionsUtils;

import java.io.File;

import butterknife.OnClick;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.RuntimePermissions;

/**
 * 描述：下载文件
 * Created by rlw on 2019/3/4.
 */
@RuntimePermissions
public class DownLoadActivity extends MVPBaseActivity<DownLoadContract.IDownloadView, DownLoadPresenter> implements DownLoadContract.IDownloadView {
    @Override
    protected int getLayoutId() {
        return R.layout.activity_download_layout;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData(@Nullable Bundle savedInstanceState) {

    }

    @Override
    protected void initNet() {

    }

    @Override
    public void showSuccess(File file) {
        //进度条退出
        NotificationUtils.cancelProgress(2);
    }

    @Override
    public void showFailureView(int code, String msg) {
        super.showFailureView(code, msg);
        //进度条退出
        NotificationUtils.cancelProgress(2);
    }

    @OnClick({R.id.download, R.id.download2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.download:
                DownLoadActivityPermissionsDispatcher.readWriteStorageWithCheck(this);
                break;
            case R.id.download2:
//                NotificationUtils.showNotificationProgress(this, 3);
//                // 下载文件
//                String path = "http://download.sdk.mob.com/apkbus.apk";
//                mPresenter.downloadFile(path);
                break;
        }

    }

    @NeedsPermission({Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE})
    void readWriteStorage() {
        // 获取到权限之后执行的操作
        NotificationUtils.showNotificationProgress(this, 2);
        // 下载文件
        String url = "http://download.sdk.mob.com/apkbus.apk";
        mPresenter.downloadFile(url);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        DownLoadActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }

    @OnShowRationale({Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE})
    void showReadWriteStorage(final PermissionRequest request) {
        PermissionsUtils.showPermissionsMessage(this, request);
    }

    @OnPermissionDenied({Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE})
    void deniedReadWriteStorage() {
        Toast.makeText(this, "已拒绝一个或以上权限", Toast.LENGTH_SHORT).show();
    }

    @OnNeverAskAgain({Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE})
    void neverReadWriteStorage() {
        PermissionsUtils.askForPermission(this, "缺少文件读写权限， 请去设置界面打开");
    }

}
